class Node {
    constructor(value) {
      this.value = value;
      this.left = null;
      this.right = null;
    }
  }
  
  // 前序遍历
  function preOrderTraversal(node) {
    if (node) {
      console.log(node.value);
      preOrderTraversal(node.left);
      preOrderTraversal(node.right);
    }
  }
  
  // 中序遍历
  function inOrderTraversal(node) {
    if (node) {
      inOrderTraversal(node.left);
      console.log(node.value);
      inOrderTraversal(node.right);
    }
  }
  
  // 后序遍历
  function postOrderTraversal(node) {
    if (node) {
      postOrderTraversal(node.left);
      postOrderTraversal(node.right);
      console.log(node.value);
    }
  }

//   层序遍历
function fn(root){
    const queue = [root]
    while(queue.length){
        let node = queue.shift()
        console.log(node.value)
        if(node.left){
            queue.push(node.left)
        }
        if(node.right){
            queue.push(node.right)
        }
    }
}
  
  // 构建一个树
  const root = new Node(1);
  root.left = new Node(2);
  root.right = new Node(3);
  root.left.left = new Node(4);
  root.left.right = new Node(5);
  
  console.log("前序遍历结果：");
  preOrderTraversal(root);
  
  console.log("中序遍历结果：");
  inOrderTraversal(root);
  
  console.log("后序遍历结果：");
  postOrderTraversal(root);

  console.log("层序遍历结果：");
  fn(root)